//
//  StatisticsChildViewController.m
//  rrqs
//
//  Created by 朱耀春 on 2019/5/3.
//

#import "StatisticsChildViewController.h"
#import "StatisticsCollectionViewCell.h"
#import "EvaluateViewController.h"
#import "NSString+Helper.h"

@interface StatisticsChildViewController ()
<UICollectionViewDelegate,
UICollectionViewDataSource>
{
    NSDictionary *dataDic;
}

@property (weak, nonatomic) IBOutlet UICollectionView *mainCollectionView;
@property (nonatomic, strong)  NSArray * titleArr;

@end

@implementation StatisticsChildViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _mainCollectionView.delegate  = self;
    _mainCollectionView.dataSource = self;
    
    [self queryData:@""];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(UpdateData:) name:@"UpdateData" object:nil];
}
//释放通知
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)UpdateData:(NSNotification *)not{
    [self queryData:[NSString stringWithFormat:@"%ld",[NSString getIntegerwithTime:not.object]]];
}

-(NSArray *)titleArr{
    if (!_titleArr ) {
        _titleArr = @[@[@"客户满意率",@"商家综合评分"],
                      @[@"配送准时率",@"平均配送时长(分钟)",@"送货距离(km)"],
                      @[@"配送单量(单)",@"一般超时(单)",@"严重超时(单)"],];
    }
    return _titleArr;
}
#pragma mark 设置collectionView
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    StatisticsCollectionViewCell * Cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"StatisticsCollectionViewCell" forIndexPath:indexPath];
    Cell.leftBottonL.text = self.titleArr[indexPath.section][indexPath.row];
    if (dataDic.count > 0 ) {
        
        if (indexPath.section == 0 && indexPath.row == 0 ) {
            Cell.leftL.text = [NSString stringWithFormat:@"%@", dataDic[@"manyiarr"][@"manyi"]];
            Cell.rightL.text =[NSString stringWithFormat:@"%@", dataDic[@"manyiarr"][@"manyipaiming"]];
        }else if (indexPath.section == 0 && indexPath.row == 1 ){
            Cell.leftL.text = [NSString stringWithFormat:@"%@",dataDic[@"zonghearr"][@"zonghe"]];
            Cell.rightL.text = [NSString stringWithFormat:@"%@",dataDic[@"zonghearr"][@"zonghepaiming"]];
        }else if (indexPath.section == 1 && indexPath.row == 0 ){
            Cell.leftL.text = [NSString stringWithFormat:@"%@", dataDic[@"zhunshiarr"][@"zhunshi"]];
            Cell.rightL.text = [NSString stringWithFormat:@"%@",dataDic[@"zhunshiarr"][@"zhunshipaiming"]];
        }else if (indexPath.section == 1 && indexPath.row == 1 ){
            Cell.leftL.text = [NSString stringWithFormat:@"%@", dataDic[@"shichangarr"][@"shichang"]];
            Cell.rightL.text = [NSString stringWithFormat:@"%@",dataDic[@"shichangarr"][@"shichangaiming"]];
        }else if (indexPath.section == 1 && indexPath.row == 2 ){
            Cell.leftL.text = [NSString stringWithFormat:@"%@",dataDic[@"juliarr"][@"juli"]];
            Cell.rightL.text = [NSString stringWithFormat:@"%@",dataDic[@"juliarr"][@"julipaiming"]];
        }else if (indexPath.section == 2 && indexPath.row == 0 ){
            Cell.leftL.text = [NSString stringWithFormat:@"%@",dataDic[@"danliangarr"][@"danliang"]];
            Cell.rightL.text = [NSString stringWithFormat:@"%@",dataDic[@"danliangarr"][@"danliangpaiming"]];
        }else if (indexPath.section == 2 && indexPath.row == 1 ){
            Cell.leftL.text = [NSString stringWithFormat:@"%@",dataDic[@"yibanarr"][@"yiban"]];
            Cell.rightL.text = [NSString stringWithFormat:@"%@",dataDic[@"yibanarr"][@"yibanpaiming"]];
        }else if (indexPath.section == 2 && indexPath.row == 2 ){
            Cell.leftL.text = [NSString stringWithFormat:@"%@",dataDic[@"yanzhongarr"][@"yanzhong"]];
            Cell.rightL.text = [NSString stringWithFormat:@"%@",dataDic[@"yanzhongarr"][@"yanzhongpaiming"]];
        }
        
    }
    if (indexPath.section == 0 ) {
        Cell.rightIcon.hidden = NO;
    }else{
        Cell.rightIcon.hidden = YES;
    }
    if (indexPath.section == 1 && indexPath.row == 1) {
        Cell.rightL_rightLayout.constant = 30;
    }
    return Cell;
}


/** 总共多少组*/
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 3;
}
#pragma mark UICollectionViewDelegate   items 个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return 2;
    }
    return 3;
}
#pragma mark item 大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    float height =   100 * kHeightScale;
    if (indexPath.section == 0 ) {
        float width = (kMainScreenWidth - 1) / 2 ;
        return CGSizeMake(width ,height );
    }
    float width = (kMainScreenWidth - 1) / 3 ;
    return CGSizeMake(width ,height );
    
}
#pragma mark 设置每section个上下左右相距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(10, 0, 10, 0);
}
#pragma mark 设置最小行间距，也就是前一行与后一行的中间最小间隔
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}
#pragma mark 设置最小列间距，也就是左行与右一行的中间最小间隔
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

#pragma mark 点击collectionView
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0 ) {
        
         [self.navigationController pushViewController:NewFunctionStoryboard(@"EvaluateViewController") animated:YES];
    }
}

- (void)queryData:(NSString *)time {
//    NSInteger time =  [NSString getIntegerwithTime:[NSString getCurrentTimesSimplify:NO]];
    NSDictionary* arg = @{@"ince":@"get_one_qishi_total",
                          @"time":time,
                          @"status":_status,
                    @"emp_id":self.Identity.userInfo.userID};
    NSLog(@"参数： %@",arg);
    [DBNet PostURL:kHost parameters:arg succeed:^(NSDictionary *data) {
        [WMHelper outPutJsonString:data];
        NSInteger flag = [[data objectForKey:@"status"]integerValue];
        if(flag == 1){
            dataDic = data;
            [_mainCollectionView reloadData];
        }else{
            [self alertHUD:[data objectForKey:@"fail"]];
        }
    } failure:^(NSError *error) {
        [self alertHUD:@"网络异常"];
    }];
}





@end
